//  import { _decorator, Component, Node, Enum, BlockInputEvents, log } from 'cc';

import { _decorator, Component, Node, Graphics } from 'cc';
import * as cc from 'cc';
const { ccclass, property } = _decorator;

import EventMgr from "../event/EventMgr";
import DialogMgr from "./DialogMgr";
import ManagerBase from '../manager/ManagerBase';

/**
 * 对话框基类
 * @author 神兽白泽
 * @description 交流群：415468592
 */
@ccclass('DialogBase')//('DialogBase')
export default class DialogBase extends Component {
    @property({ displayName: "吞噬触摸" })
    private swallow_touch: boolean = true;
    @property({ displayName: "任意触摸关闭" })
    private touch_off: boolean = false;
    @property({
        type: Node, displayName: "屏蔽触摸区域", visible() {
            return this.touch_off;
        }
    })
    private touch_block: Node | null = null;
    /** 最底层蒙版 - 全屏的 */
    private _node_mask: Node | null = null;
    /** 界面数据 */
    protected dialog_data: any = null;

    /**
    * 初始化界面数据
    * @param param 数据
    */
    public initDialogData(param?: any) {
        this.dialog_data = param;
        this.initEvent();
    }

    /**
    * 初始化关闭事件
    * - 覆盖全屏范围
    */
    private initEvent() {
        if (!this.touch_off && !this.swallow_touch) return; // 未开启
        if (cc.isValid(this._node_mask)) return; // 已开启 已监听
        // 初始化节点
        this._node_mask = new cc.Node('node_mask');
        this._node_mask.addComponent(cc.Button);
        this._node_mask.on("click", this.onClickMask, this);
        if (cc.isValid(this.touch_block)) {
            this.touch_block.addComponent(cc.BlockInputEvents);
        }
        const size = new cc.Size(this.node.getComponent(cc.UITransform).width, this.node.getComponent(cc.UITransform).height)
        // 绘图组件 - 绘制半透明蒙版
        let comp_graphics = this._node_mask.addComponent(cc.Graphics);
        this.initGraphicsMask(comp_graphics, size)
        // 层级、尺寸、透明度
        this._node_mask.getComponent(cc.UITransform).setContentSize(size)
        this.node.insertChild(this._node_mask, -10000);
    }

    /**
    * 初始化绘图组件
    */
    private initGraphicsMask(comp_graphics: Graphics, size: cc.Size) {
        comp_graphics.clear();
        // comp_graphics.node.addComponent(cc.UIOpacity).opacity = 1;
        comp_graphics.fillColor = cc.color(0, 0, 0, 120);
        comp_graphics.fillRect(-size.width / 2, -size.height / 2, size.width * 2, size.height * 2);// 绘制矩形路径，矩形坐标，宽高
    }

    /**
    * 点击到蒙版回调
    */
    private onClickMask() {
        if (this.touch_off) {
            this.closeDialog();
        }
    }
    /**
    * @description 关闭弹窗
    */
    public closeDialog() {
        DialogMgr.closeDialog(this.node.name as any);
    }
    public onDestroy() {
        EventMgr.offAll(this);
        ManagerBase.clearTarget(this);
    }
}
